#include <slothttpd/logger.hpp>
#include <slothttpd/config/configuration.hpp>

#include <cstring>
#include <ctime>

#include <fstream>
#include <ios>

#include <iostream>
/*
#include <boost/iostreams/device/file.hpp>
#include <boost/iostreams/stream.hpp>
*/
namespace slothttpd {

//namespace io = boost::iostreams;

class log_stream :
        public std::ofstream
{
public:
    inline explicit log_stream (const char *filename) :
        std::ofstream(filename, std::ios_base::app | std::ios_base::out) // | std::ios_base::trunc)
    {
        std::time_t time = std::time(NULL);
        
        (*this) << "\n\n --- New log ---\n"
                << std::ctime(&time) << std::endl;
    }
};

const char *basename(const char *path)
{
    static const char *def = "<unknown>";
    
    const char *r = ::strrchr(path, '/');
    
    return (r == nullptr) ? def : r + 1;
}

std::ostream &logger_helper(long line, const char *file)
{
    using namespace slothttpd::config;
    
    //static io::stream_buffer
    
    static log_stream ofile(configuration::instance->log_file.c_str());
    
    //return std::cerr;
    // TODO: print timestamp
    return ofile << basename(file) << ":" << line << ": ";
}



}
